package org.rails.extjs.javascript;

import org.rails.extjs.AbsBase;
import org.rails.extjs.Component;
import org.rails.extjs.javascript.JavaScript;

public class JSUtils {
	
	public static JavaScript addItem(String id, Component component){
		JavaScript js = new JavaScript();
		String tempName = createTempName();
		js.append("var {0}=Ext.getCmp('{1}');",tempName,id);
		js.append("{0}.add({1});",tempName,component.getScript());
		return js;
	}
	
	public static JavaScript insertItem(String id,int itemIndex, Component component){
		JavaScript js = new JavaScript();
		String tempName = createTempName();
		js.append("var {0}=Ext.getCmp('{1}');",tempName,id);
		js.append("{0}.insert({1},{2});",tempName,itemIndex,component.getScript());
		return js;
	}
	
	public static JavaScript updateItem(String id,int itemIndex, Component component){
		JavaScript js = new JavaScript();
		String tempName = createTempName();
		js.append("var {0}=Ext.getCmp('{1}');",tempName,id);
		js.append("{0}.remove({0}.get({1}));",tempName,itemIndex);
		js.append("{0}.insert({1},{2});",tempName,itemIndex,component.getScript());
		return js;
	}
	
	public static JavaScript updateItem(String id,String itemId, Component component){
		JavaScript js = new JavaScript();
		String tempName = createTempName();
		String index = createTempName();
		js.append("var {0}=Ext.getCmp('{1}');",tempName,id);
		js.append("var {0}={1}.items.indexOf(Ext.getCmp('{2}'));",index,tempName,itemId);		
		js.append("{0}.remove('{1}');",tempName,itemId);
		js.append("{0}.insert({1},{2});",tempName,index,component.getScript());
		return js;
	}
	
	public static JavaScript hideItem(String id,int itemIndex){
		JavaScript js = new JavaScript();
		String tempName = createTempName();
		js.append("var {0}=Ext.getCmp('{1}');",tempName,id);
		js.append("{0}.get({1}).hide();",tempName,itemIndex);
		return js;
	}
	
	public static JavaScript hide(String id){
		JavaScript js = new JavaScript();
		String tempName = createTempName();
		js.append("var {0}=Ext.getCmp('{1}');",tempName,id);
		js.append("{0}.hide();",tempName);
		return js;
	}
	
	public static JavaScript removeItem(String id,int itemIndex){
		JavaScript js = new JavaScript();
		String tempName = createTempName();
		js.append("var {0}=Ext.getCmp('{1}');",tempName,id);
		js.append("{0}.remove({0}.get({1}));",tempName,itemIndex);
		return js;
	}	
	
	public static JavaScript removeItem(String id,String itemId){
		JavaScript js = new JavaScript();
		String tempName = createTempName();
		js.append("var {0}=Ext.getCmp('{1}');",tempName,id);
		js.append("{0}.remove('{1}');",tempName,itemId);
		return js;
	}	
	
	public static JavaScript removeAllItem(String id){
		JavaScript js = new JavaScript();
		String tempName = createTempName();
		js.append("var {0}=Ext.getCmp('{1}');",tempName,id);
		js.append("{0}.removeAll();",tempName);
		return js;
	}
	
	public static JavaScript destroy(String id){
		JavaScript js = new JavaScript();
		String tempName = createTempName();
		js.append("var {0}=Ext.getCmp('{1}');",tempName,id);
		js.append("{0}.destroy();",tempName);
		return js;
	}
	
	public static JavaScript destroy(String targetId,int itemIndex){
		JavaScript js = new JavaScript();
		String tempName = createTempName();
		js.append("var {0}=Ext.getCmp('{1}');",tempName,targetId);
		js.append("{0}.get({1}).destroy();",tempName,itemIndex);
		return js;
	}
	
	public static JavaScript doLayout(String id){
		return new JavaScript("Ext.getCmp('{0}').doLayout();",id);
	}
	
	public static String createTempName(){
		return "Temp_" + AbsBase.createRandom(6);
	}

}
